package com.alibaba.tdata.sever;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.tdata.aisp.server.common.constant.AnalyseTaskStatusEnum;
import com.alibaba.tdata.aisp.server.common.utils.TaskCacheUtil;
import com.alibaba.tdata.aisp.server.repository.domain.TaskDO;

import org.junit.Test;
import org.springframework.util.CollectionUtils;

/**
 * @ClassName: AnalyseTaskServiceTest
 * @Author: dyj
 * @DATE: 2022-04-28
 * @Description:
 **/
public class AnalyseTaskServiceTest {
    @Test
    public void queryAdLine() {
        Map<String, List<JSONArray>> lineMap = new HashMap<>();
        JSONObject taskRes = queryTaskRes();
        if (CollectionUtils.isEmpty(taskRes)) {
            return ;
        }
        if (taskRes.containsKey("requestParam") && taskRes.getJSONObject("requestParam").containsKey("series")){
            JSONArray series = taskRes.getJSONObject("requestParam").getJSONArray("series");
            List<JSONArray> seriesLine = new LinkedList<>();
            int size = series.size();
            for (int i=0; i<size; i++){
                JSONArray a = series.getJSONArray(i);
                a.set(0, a.getLong(0)*1000);
                seriesLine.add(a);
            }
            lineMap.put("origin", seriesLine);
        }

        if (taskRes.containsKey("response") && taskRes.getJSONObject("response").containsKey("data")
            && taskRes.getJSONObject("response").getJSONObject("data").containsKey("detectSeries")) {
            List<JSONArray> ads = new LinkedList<>();
            List<JSONArray> ups = new LinkedList<>();
            List<JSONArray> lows = new LinkedList<>();
            JSONArray cols = taskRes.getJSONObject("response").getJSONObject("data").getJSONArray("detectSeriesColumns");
            int tsIdx = cols.indexOf("timestamp");
            int vIdx = cols.indexOf("value");
            int anomalyIdx = cols.indexOf("anomaly");
            int upIdx = cols.indexOf("upperbound");
            int lowIdx = cols.indexOf("lowerbound");
            JSONArray detectSeries = taskRes.getJSONObject("response").getJSONObject("data").getJSONArray("detectSeries");
            System.out.println(detectSeries);
            int size = detectSeries.size();
            for (int i=0; i<size; i++){
                JSONArray d = detectSeries.getJSONArray(i);
                if (d.getBoolean(anomalyIdx)) {
                    long timestamp = d.getLong(tsIdx) * 1000;

                    JSONArray ad = new JSONArray();
                    ad.set(0, timestamp);
                    ad.set(1, d.getFloat(vIdx));
                    ads.add(ad);

                    JSONArray up = new JSONArray();
                    up.set(0, timestamp);
                    up.set(1, d.getFloat(upIdx));
                    ups.add(up);

                    JSONArray low = new JSONArray();
                    low.set(0, timestamp);
                    low.set(1, d.getFloat(lowIdx));
                    lows.add(low);
                }
            }
            lineMap.put("ads", ads);
            lineMap.put("ups", ups);
            lineMap.put("lows", lows);
            System.out.println(ads);
            System.out.println(ups);
            System.out.println(lows);
        }
        return ;
    }

    public JSONObject queryTaskRes() {
        JSONObject result = new JSONObject();
        String str = "{\n"
            + "  \"response\": {\n"
            + "    \"data\": {\n"
            + "      \"modelParam\": {\n"
            + "        \"hisDetectResult\": {\n"
            + "          \"abnormalDataAlgo\": []\n"
            + "        },\n"
            + "        \"test\": \"test\",\n"
            + "        \"cacheParam\": {\n"
            + "          \"cachePreprocessParam\": {\n"
            + "            \"period_stl\": {\n"
            + "              \"period_shift\": true,\n"
            + "              \"stl_smooth_win\": 0\n"
            + "            },\n"
            + "            \"interval_ms\": 60000,\n"
            + "            \"detect_direction\": \"both\",\n"
            + "            \"train_ts_end\": \"2022-01-04 01:11:00\",\n"
            + "            \"detect_ts_end\": \"2022-01-04 01:12:00\",\n"
            + "            \"diff\": {},\n"
            + "            \"detect_ts_start\": \"2022-01-04 01:12:00\",\n"
            + "            \"smoothing\": {\n"
            + "              \"func\": \"median\",\n"
            + "              \"smoothing_window_size\": 1\n"
            + "            },\n"
            + "            \"train_ts_start\": \"2022-01-04 00:00:00\",\n"
            + "            \"drop_noise\": {\n"
            + "              \"ab_ts_list\": [],\n"
            + "              \"normal_data\": [],\n"
            + "              \"auto_delete_anomaly\": 0,\n"
            + "              \"continuous_ab_retrain\": 3,\n"
            + "              \"n\": 5,\n"
            + "              \"abnormal_data\": []\n"
            + "            },\n"
            + "            \"fillna\": {\n"
            + "              \"fillvalue\": true\n"
            + "            }\n"
            + "          },\n"
            + "          \"cacheDetectParam\": {\n"
            + "            \"sigma\": 1826.475830073455,\n"
            + "            \"avg_method\": \"mean\",\n"
            + "            \"sigma_method\": \"stddev\",\n"
            + "            \"n_down\": 3.4798361101699395,\n"
            + "            \"anomaly_cnt\": 1,\n"
            + "            \"n\": 0,\n"
            + "            \"colname\": \"kpi\",\n"
            + "            \"mean\": 12919.597778320312,\n"
            + "            \"narrow_pulse_detect\": true,\n"
            + "            \"return_origin_series\": false,\n"
            + "            \"return_bounds\": true,\n"
            + "            \"added_n\": 2,\n"
            + "            \"narrow_pulse_exist\": false,\n"
            + "            \"direction\": \"both\",\n"
            + "            \"n_up\": 4.356424833038495\n"
            + "          },\n"
            + "          \"cacheReduceParam\": {},\n"
            + "          \"cacheStlData\": []\n"
            + "        }\n"
            + "      },\n"
            + "      \"detectSeriesColumns\": [\n"
            + "        \"timestamp\",\n"
            + "        \"value\",\n"
            + "        \"anomaly\",\n"
            + "        \"upperbound\",\n"
            + "        \"lowerbound\"\n"
            + "      ],\n"
            + "      \"detectSeries\": [\n"
            + "        [\n"
            + "          1641229920,\n"
            + "          571.18359375,\n"
            + "          1,\n"
            + "          20876.502441396908,\n"
            + "          6563.76123047809\n"
            + "        ]\n"
            + "      ]\n"
            + "    }\n"
            + "  },\n"
            + "  \"requestParam\": {\n"
            + "    \"taskType\": \"sync\",\n"
            + "    \"series\": [\n"
            + "      [\n"
            + "        1641225600,\n"
            + "        15391.1337890625\n"
            + "      ],\n"
            + "      [\n"
            + "        1641225660,\n"
            + "        17223.55078125\n"
            + "      ],\n"
            + "      [\n"
            + "        1641225720,\n"
            + "        15635.00390625\n"
            + "      ],\n"
            + "      [\n"
            + "        1641225780,\n"
            + "        16146.3125\n"
            + "      ],\n"
            + "      [\n"
            + "        1641225840,\n"
            + "        15914.775390625\n"
            + "      ],\n"
            + "      [\n"
            + "        1641225900,\n"
            + "        15924.4794921875\n"
            + "      ],\n"
            + "      [\n"
            + "        1641225960,\n"
            + "        15791.970703125\n"
            + "      ],\n"
            + "      [\n"
            + "        1641226020,\n"
            + "        15637.287109375\n"
            + "      ],\n"
            + "      [\n"
            + "        1641226080,\n"
            + "        15546.31640625\n"
            + "      ],\n"
            + "      [\n"
            + "        1641226140,\n"
            + "        15490.6123046875\n"
            + "      ],\n"
            + "      [\n"
            + "        1641226200,\n"
            + "        15250.00390625\n"
            + "      ],\n"
            + "      [\n"
            + "        1641226260,\n"
            + "        15079.779296875\n"
            + "      ],\n"
            + "      [\n"
            + "        1641226320,\n"
            + "        14856.2958984375\n"
            + "      ],\n"
            + "      [\n"
            + "        1641226380,\n"
            + "        14932.2587890625\n"
            + "      ],\n"
            + "      [\n"
            + "        1641226440,\n"
            + "        14805.875\n"
            + "      ],\n"
            + "      [\n"
            + "        1641226500,\n"
            + "        14736.8876953125\n"
            + "      ],\n"
            + "      [\n"
            + "        1641226560,\n"
            + "        14527.7705078125\n"
            + "      ],\n"
            + "      [\n"
            + "        1641226620,\n"
            + "        14354.75390625\n"
            + "      ],\n"
            + "      [\n"
            + "        1641226680,\n"
            + "        14310.7373046875\n"
            + "      ],\n"
            + "      [\n"
            + "        1641226740,\n"
            + "        14398.283203125\n"
            + "      ],\n"
            + "      [\n"
            + "        1641226800,\n"
            + "        14188.2705078125\n"
            + "      ],\n"
            + "      [\n"
            + "        1641226860,\n"
            + "        14047.7919921875\n"
            + "      ],\n"
            + "      [\n"
            + "        1641226920,\n"
            + "        13847.2412109375\n"
            + "      ],\n"
            + "      [\n"
            + "        1641226980,\n"
            + "        13849.4912109375\n"
            + "      ],\n"
            + "      [\n"
            + "        1641227040,\n"
            + "        13716.3876953125\n"
            + "      ],\n"
            + "      [\n"
            + "        1641227100,\n"
            + "        13727.875\n"
            + "      ],\n"
            + "      [\n"
            + "        1641227160,\n"
            + "        13450.43359375\n"
            + "      ],\n"
            + "      [\n"
            + "        1641227220,\n"
            + "        13364.375\n"
            + "      ],\n"
            + "      [\n"
            + "        1641227280,\n"
            + "        13340.783203125\n"
            + "      ],\n"
            + "      [\n"
            + "        1641227340,\n"
            + "        13295.099609375\n"
            + "      ],\n"
            + "      [\n"
            + "        1641227400,\n"
            + "        13307.30859375\n"
            + "      ],\n"
            + "      [\n"
            + "        1641227460,\n"
            + "        13116.8916015625\n"
            + "      ],\n"
            + "      [\n"
            + "        1641227520,\n"
            + "        12910.0830078125\n"
            + "      ],\n"
            + "      [\n"
            + "        1641227580,\n"
            + "        12960.095703125\n"
            + "      ],\n"
            + "      [\n"
            + "        1641227640,\n"
            + "        12862.2373046875\n"
            + "      ],\n"
            + "      [\n"
            + "        1641227700,\n"
            + "        12862.5498046875\n"
            + "      ],\n"
            + "      [\n"
            + "        1641227760,\n"
            + "        12657.1455078125\n"
            + "      ],\n"
            + "      [\n"
            + "        1641227820,\n"
            + "        12521.779296875\n"
            + "      ],\n"
            + "      [\n"
            + "        1641227880,\n"
            + "        12514.9501953125\n"
            + "      ],\n"
            + "      [\n"
            + "        1641227940,\n"
            + "        12516.19140625\n"
            + "      ],\n"
            + "      [\n"
            + "        1641228000,\n"
            + "        12446.8876953125\n"
            + "      ],\n"
            + "      [\n"
            + "        1641228060,\n"
            + "        12372.3037109375\n"
            + "      ],\n"
            + "      [\n"
            + "        1641228120,\n"
            + "        12127.68359375\n"
            + "      ],\n"
            + "      [\n"
            + "        1641228180,\n"
            + "        12173.337890625\n"
            + "      ],\n"
            + "      [\n"
            + "        1641228240,\n"
            + "        12059.5751953125\n"
            + "      ],\n"
            + "      [\n"
            + "        1641228300,\n"
            + "        12062.662109375\n"
            + "      ],\n"
            + "      [\n"
            + "        1641228360,\n"
            + "        11919.7705078125\n"
            + "      ],\n"
            + "      [\n"
            + "        1641228420,\n"
            + "        11726.650390625\n"
            + "      ],\n"
            + "      [\n"
            + "        1641228480,\n"
            + "        11739.5205078125\n"
            + "      ],\n"
            + "      [\n"
            + "        1641228540,\n"
            + "        11717.7587890625\n"
            + "      ],\n"
            + "      [\n"
            + "        1641228600,\n"
            + "        11590.25390625\n"
            + "      ],\n"
            + "      [\n"
            + "        1641228660,\n"
            + "        11563.7705078125\n"
            + "      ],\n"
            + "      [\n"
            + "        1641228720,\n"
            + "        11325.775390625\n"
            + "      ],\n"
            + "      [\n"
            + "        1641228780,\n"
            + "        11310.12890625\n"
            + "      ],\n"
            + "      [\n"
            + "        1641228840,\n"
            + "        11256.224609375\n"
            + "      ],\n"
            + "      [\n"
            + "        1641228900,\n"
            + "        11374.6044921875\n"
            + "      ],\n"
            + "      [\n"
            + "        1641228960,\n"
            + "        11158.8876953125\n"
            + "      ],\n"
            + "      [\n"
            + "        1641229020,\n"
            + "        11025.8330078125\n"
            + "      ],\n"
            + "      [\n"
            + "        1641229080,\n"
            + "        10919.87890625\n"
            + "      ],\n"
            + "      [\n"
            + "        1641229140,\n"
            + "        10904.4375\n"
            + "      ],\n"
            + "      [\n"
            + "        1641229200,\n"
            + "        10396.154296875\n"
            + "      ],\n"
            + "      [\n"
            + "        1641229260,\n"
            + "        11365.5498046875\n"
            + "      ],\n"
            + "      [\n"
            + "        1641229320,\n"
            + "        10653.4873046875\n"
            + "      ],\n"
            + "      [\n"
            + "        1641229380,\n"
            + "        10631.37890625\n"
            + "      ],\n"
            + "      [\n"
            + "        1641229440,\n"
            + "        10604.5458984375\n"
            + "      ],\n"
            + "      [\n"
            + "        1641229500,\n"
            + "        10698.5458984375\n"
            + "      ],\n"
            + "      [\n"
            + "        1641229560,\n"
            + "        10521.904296875\n"
            + "      ],\n"
            + "      [\n"
            + "        1641229620,\n"
            + "        10349.93359375\n"
            + "      ],\n"
            + "      [\n"
            + "        1641229680,\n"
            + "        10331.25390625\n"
            + "      ],\n"
            + "      [\n"
            + "        1641229740,\n"
            + "        10329.3916015625\n"
            + "      ],\n"
            + "      [\n"
            + "        1641229800,\n"
            + "        10325.1669921875\n"
            + "      ],\n"
            + "      [\n"
            + "        1641229860,\n"
            + "        10216.712890625\n"
            + "      ],\n"
            + "      [\n"
            + "        1641229920,\n"
            + "        571.18359375\n"
            + "      ]\n"
            + "    ],\n"
            + "    \"algoParam\": {\n"
            + "      \"returnBounds\": true\n"
            + "    },\n"
            + "    \"modelParam\": {\n"
            + "      \"test\": \"test\"\n"
            + "    },\n"
            + "    \"entityId\": \"test\"\n"
            + "  },\n"
            + "  \"status\": \"success\"\n"
            + "}";
        result = JSONObject.parseObject(str);
        return result;
    }
}
